//
//  U9WebViewProgress.m
//  iMobile
//
//  Created by baken on 2016/12/9.
//  Copyright © 2016年 MyApp. All rights reserved.
//

#import "U9WebViewProgress.h"


NSString *completeRPCURL = @"webviewprogressproxy:///complete";


const float IMInitialProgressValue = 0.1f;
const float IMInteractiveProgressValue = 0.5f;
const float IMFinalProgressValue = 0.9f;

@interface U9WebViewProgress()

@property (nonatomic, assign) NSInteger loadingCount;

@property (nonatomic, assign) NSInteger maxLoadCount;

@property (nonatomic, assign) BOOL interactive;

@property (nonatomic, strong) NSURL *currentURL;
@end

@implementation U9WebViewProgress

- (instancetype)init{
    if (self = [super init]) {
        _maxLoadCount = _loadingCount = 0;
        _interactive = NO;
    }
    return self;
}

- (void)startProgress{
    if (_progress < IMInitialProgressValue) {
        [self setProgress:IMInteractiveProgressValue];
    }
}


- (void)incrementProgress{
    float progress = self.progress;
    float maxProgress = _interactive? IMFinalProgressValue :IMInteractiveProgressValue;
    float remainPercent = (float)_loadingCount / (float)_maxLoadCount;
    float increment = (maxProgress - progress) *remainPercent;
    progress += increment;
    progress = fmin(progress, maxProgress);
    [self setProgress:progress];
}

- (void)completeProgress{
    [self setProgress:1.0];
}

- (void)setProgress:(float)progress{
    if (progress > _progress || progress == 0) {
        _progress = progress;
        
    }
}


- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{

//    if ([webView.URL.absoluteString isEqualToString:completeRPCURL]) {
//        [self completeProgress];
//        return NO;
//    }
//    
//    BOOL ret = YES;
//    if ([_webViewProxyDelegate respondsToSelector:@selector(webView:shouldStartLoadWithRequest:navigationType:)]) {
//        ret = [_webViewProxyDelegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
//    }
//    
//    BOOL isFragmentJump = NO;
//    if (request.URL.fragment) {
//        NSString *nonFragmentURL = [request.URL.absoluteString stringByReplacingOccurrencesOfString:[@"#" stringByAppendingString:request.URL.fragment] withString:@""];
//        isFragmentJump = [nonFragmentURL isEqualToString:webView.request.URL.absoluteString];
//    }
//    
//    BOOL isTopLevelNavigation = [request.mainDocumentURL isEqual:request.URL];
//    
//    BOOL isHTTP = [request.URL.scheme isEqualToString:@"http"] || [request.URL.scheme isEqualToString:@"https"];
//    if (ret && !isFragmentJump && isHTTP && isTopLevelNavigation) {
//        _currentURL = request.URL;
//        [self reset];
//    }
//    return ret;

}


- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
    if ([_webViewProxyDelegate respondsToSelector:@selector(webView:didCommitNavigation:)]) {
        [_webViewProxyDelegate webView:webView didCommitNavigation:navigation];
    }
    _loadingCount++;
    _maxLoadCount = fmax(_maxLoadCount, _loadingCount);
    
    [self startProgress];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    if ([_webViewProxyDelegate respondsToSelector:@selector(webView:didFinishNavigation:)]) {
        [_webViewProxyDelegate webView:webView didFinishNavigation:navigation];
    }
    _loadingCount--;
    [self incrementProgress];
    __block NSString *readyState;
    [webView evaluateJavaScript:@"document.readyState" completionHandler:^(id response, NSError * _Nullable error) {
        readyState = response;
    }];
    
    BOOL interactive = [readyState isEqualToString:@"interactive"];
    if (interactive) {
        _interactive = YES;
        NSString *waitForCompleteJS = [NSString stringWithFormat:@"window.addEventListener('load',function() { var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = '%@'; document.body.appendChild(iframe);  }, false);", completeRPCURL];
        [webView evaluateJavaScript:waitForCompleteJS completionHandler:^(id response, NSError * _Nullable error) {
            
        }];
    }
    
//    BOOL isNotRedirect = _currentURL && [_currentURL isEqual:webView.request.mainDocumentURL];
//    BOOL complete = [readyState isEqualToString:@"complete"];
//    if (complete && isNotRedirect) {
//        [self completeProgress];
//    }
//
    
    
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    
}







































@end
































